# -*- coding: utf-8 -*-
# @FileName : application.py
# @Time     : 2023/6/8 19:14
# @Author   : Zhong Runke
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_cors import CORS
import redis
import pymysql
pymysql.install_as_MySQLdb()

class Application(Flask):
    def __init__(self, import_name, template_folder=None, instance_relative_config=False):
        super(Application, self).__init__(import_name, template_folder=template_folder, static_folder=None, instance_relative_config=instance_relative_config)
        self.config.from_pyfile("config/base_setting.py")
        if 'ops_config' in os.environ:
            self.config.from_pyfile("config/%s_setting.py" % os.environ['ops_config'])

        # 数据库加载
        db.init_app(self)
        migrate.init_app(self, db)
        # 跨域注册
        cors.init_app(self)
        # 注册模型
        from common import models
root_path = os.getcwd()
print(root_path)
# MySQL数据库
db = SQLAlchemy()
# 数据库迁移工具
migrate = Migrate()
# 跨域解决
cors = CORS()
# 创建app
app = Application(__name__, template_folder=root_path + "/web/templates")
# 项目根路径
app.root_path = root_path
# Redis数据库
r = redis.StrictRedis(host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT'], db=app.config['REDIS_NO'])
# r = redis.StrictRedis(host='120.25.1.147', port='10002', db=0)
app.secret_key = os.urandom(24)

"""函数模板"""
from common.libs.UrlManager import UrlManager
app.add_template_global(UrlManager.buildStaticUrl, 'buildStaticUrl')
app.add_template_global(UrlManager.buildUrl, 'buildUrl')
app.add_template_global(UrlManager.buildFileUrl, 'buildFileUrl')